package org.dromara.common.web.utils.jackson;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import jodd.typeconverter.Converter;

import java.io.IOException;

public class BooleanDeserializer extends JsonDeserializer<Boolean> {

    @Override
    public Boolean deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException {
        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
        Boolean flag = false;

        try {
            if (StrUtil.isNotEmpty(node.textValue())) {
                return Converter.get().toBoolean(node.textValue());
            }
        } catch (Exception e) {
        }

        return flag;
    }


    @Override
    public Boolean getNullValue(DeserializationContext ctxt) {
        return false;
    }
}
